import axios from 'axios'
import { Message } from 'element-ui'
import {
	getToken,
	getUserId
} from '@/utils/auth';
let baseURL = process.env.VUE_APP_API_HOST
if (process.env.NODE_ENV == 'development') {
	baseURL = '/dev'
}
const service = axios.create({
	baseURL: baseURL,
	timeout: 60000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(config => {
	config.headers['access-token'] = getToken()
	config.headers['user-id'] = getUserId()
	return config
}, error => {
	console.log(error) // for debug
	Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(response => {

	const { status, data } = response
	if (status !== 200) {
		Message('网络出错！！！')
		return
	}
	return data
}, error => {
	console.log('err' + error) // for debug
	return Promise.reject(error)
})
export default service